package com.example.sonone.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 单例
 */
public class SystemParamCache {
    Logger LOGGER = LoggerFactory.getLogger(SystemParamCache.class);

    private static SystemParamCache systemParamCache;

    private Map<String, String> paramMap;

    private Map<String, String> paramMapCron;

    private boolean ready = false;

    private SystemParamCache() {
    }

    public static SystemParamCache getInstance() {
        synchronized (SystemParamCache.class) {
            if (systemParamCache == null) {
                systemParamCache = new SystemParamCache();
            }
        }
        return systemParamCache;
    }

    public void reload(Map<String, String> paramMap) {
        if (null != paramMap) {
            this.paramMap = paramMap;
            this.ready = true;
            LOGGER.info("系统参数缓存加载完毕");
        }
    }

    public void reloadCron(Map<String, String> paramMap) {
        if (null != paramMap) {
            this.paramMapCron = paramMap;
            this.ready = true;
            LOGGER.info("定时 参数缓存加载完毕");
        }
    }

    public boolean isReady() {
        return this.ready;
    }

    public String getParamValue(String paramId) {
        if (null == paramMap) {
            throw new RuntimeException("fatal_cacheError");
        }
        return paramMap.get(paramId) == null ? null : paramMap.get(paramId).trim();
    }

    public String getParamValueCron(String paramId) {
        if (null == paramMapCron) {
            throw new RuntimeException("fatal_cacheError  cron");
        }
        return paramMapCron.get(paramId) == null ? null : paramMapCron.get(paramId).trim();
    }

    public boolean containsParam(String paramId) {
        return paramMap.containsKey(paramId);
    }

    public boolean containsParamCron(String paramId) {
        return paramMapCron.containsKey(paramId);
    }
}
